<?php

class Audio extends CI_Controller {

    private static $layout_data = array();
    private static $home_data = array();

    public function __construct()
    {
        parent::__construct();

        $this->load->model('node_model');
        
        self::$home_data = array();
        self::$layout_data['navigation'] = "Oneway Backend &raquo; Audio";
    }

    public function index()
    {
        //Get page params
        $parentID = 5;
        $limit = 10;
        $offset = $this->uri->segment(4)?$this->uri->segment(4):0;

        //Get arrNode data and total
        $arrNode = $this->node_model->getListNodeByParentID($parentID, $limit, $offset);

        //Check total and load paging
        if($arrNode['total'] > $limit)
        {
            $this->load->library('pagination');

            $config['base_url'] = site_url() . '/audio/index/offset/';
            $config['total_rows'] = $arrNode['total'];
            $config['per_page'] = $limit;
            $config['uri_segment'] = 4;

            $this->pagination->initialize($config);
            self::$home_data['paging'] = TRUE;
        }
        
        //Set home data
        self::$home_data['offset'] = $offset;
        self::$home_data['arrNode'] = $arrNode;

        //Assign to view
        self::$layout_data['content'] = $this->load->view('audio/index', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    /*
     * Add function
     */
    public function add()
    {
        if($this->input->post('add'))
        {
            //Set node params
            $nodeParams = array(
                'category_id'   =>  $this->input->post('category_id'),
                'title'         =>  $this->input->post('title'),
                'thumb'         =>  $this->input->post('thumb'),
                'description'   =>  $this->input->post('description'),
                'created_on'    =>  time()
            );

            //Set node field params
            $nodeFieldParams = array(
                'link'          =>  $this->input->post('link'),
                'value'         =>  $this->input->post('content')
            );

            //var_dump($nodeParams, $nodeFieldParams); exit();

            //Insert node to DB
            self::$home_data['result'] = $this->node_model->insertNode($nodeParams, $nodeFieldParams);
        }
        
        self::$layout_data['navigation'] = "Oneway Backend &raquo; Audio &raquo; Viết Bài Mới";
        self::$layout_data['content'] = $this->load->view('audio/add', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    /*
     * Edit function
     */
    public function edit()
    {
        if($this->input->post('update'))
        {
            //Get nodeID
            $nodeID = $this->input->post('node_id');
            
            //Set node params
            $nodeParams = array(
                'category_id'   =>  $this->input->post('category_id'),
                'title'         =>  $this->input->post('title'),
                'thumb'         =>  $this->input->post('thumb'),
                'description'   =>  $this->input->post('description')
            );

            //Set node field params
            $nodeFieldParams = array(
                'link'          =>  $this->input->post('link'),
                'value'         =>  $this->input->post('content')
            );

            //var_dump($nodeParams, $nodeFieldParams); exit();

            //Insert node to DB
            self::$home_data['result'] = $this->node_model->editNode($nodeID, $nodeParams, $nodeFieldParams);
        }
        
        //Get nodeID
        $nodeID = $this->uri->segment(4);

        //Get note detail
        $nodeDetail = $this->node_model->getNodeDetail($nodeID);

        //Assign to view
        self::$home_data['node'] = $nodeDetail;

        //Load view
        self::$layout_data['navigation'] = "Oneway Backend &raquo; Audio &raquo; Chỉnh Sửa Bài Viết";
        self::$layout_data['content'] = $this->load->view('audio/edit', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    /*
     * Delete function
     */
    public function delete()
    {
        //Get nodeID
        $nodeID = $this->uri->segment(4);

        //Delete node
        $this->node_model->deleteNode($nodeID);

        //Redirect
        redirect($_SERVER['HTTP_REFERER']);
    }
}
